<template>
  <div>
     <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
       <el-form-item>
         <el-input v-model="dataForm.productName" placeholder="商品名称" clearable></el-input>
       </el-form-item>
       <el-form-item>
         <el-select v-model="dataForm.categoryId" placeholder="商品分类">
           <el-option v-for="item in categoryList" :label="item.categoryName" :key="item.id" :value="item.id"></el-option>
         </el-select>
       </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" ref="productTable" style="width: 100%" border>
      <el-table-column type="selection" width="60" align="center"></el-table-column>
      <el-table-column prop="id" label="编号" width="100" align="center"></el-table-column>
      <el-table-column label="商品图片" width="120" align="center">
        <template slot-scope="scope">
          <img style="height: 80px" :src="scope.row.productImage" />
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center">
        <template slot-scope="scope">
          <p>{{scope.row.productName}}</p>
          <p>编号：{{scope.row.productSn}}</p>
        </template>
      </el-table-column>
      <el-table-column label="标签" width="140" align="center">
        <template slot-scope="scope">
          <p>
            上架：
            <el-switch @change="shangjia(scope.row)" v-model="scope.row.saleFlag"></el-switch>
          </p>
          <p>
            显示：
            <el-switch @change="shangjia(scope.row)" v-model="scope.row.showFlag"></el-switch>
          </p>
        </template>
      </el-table-column>
      <el-table-column label="价格" width="120"  align="center">
        <template slot-scope="scope" >
          <p>最低：{{scope.row.minPirce}}</p>
          <p>最高：{{scope.row.maxPrice}}</p>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary"  @click="addOrUpdateHandle(scope.row.id, 1)">查看</el-button>
          <el-button size="mini" type="warning"  @click="addOrUpdateHandle(scope.row.id, 2)">修改</el-button>
          <el-button size="mini" type="danger"  @click="deleteHandle(scope.row.id)" style="margin-top: 10px" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>
<script>
import AddOrUpdate from './list-add-update'
export default {
  data () {
    return {
      dataForm: {
        productName: '',
        categoryId: ''
      },
      categoryList: [],
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate
  },
  activated () {
    this.getDataList()
    this.getCategoryList()
  },
  methods: {
    getCategoryList () {
      this.$http({
        url: this.$http.adornUrl('/product/category/list'),
        method: 'get'
      }).then(({data}) => {
        if (data && data.status === 200) {
          this.categoryList = data.data.list
        }
      })
    },
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/product/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'productName': this.dataForm.productName,
          'categoryId': this.dataForm.categoryId
        })
      }).then(({ data }) => {
        if (data && data.status === 200) {
          
          this.dataList = data.data.list;
         
          this.totalPage = data.data.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 新增 / 修改 / 查看 flage是标识修改还是查看
    addOrUpdateHandle (id, flage) {
      flage = flage === undefined ? 0 : flage;
      
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, flage)
      })
    },
    // 删除
    deleteHandle (id) {
      var userIds = id
        ? [id]
        : this.dataListSelections.map(item => {
          return item.userId
        })
      this.$confirm(
        `确定对[id=${userIds.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`,
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          this.$http({
            url: this.$http.adornUrl('/product/delete/' + id),
            method: 'get',
            data: this.$http.adornData(userIds, false)
          }).then(({ data }) => {
            if (data && data.status === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.message)
            }
          })
        })
        .catch(() => {})
    },
    shangjia () {}
  }
}
</script>
